package com.shop.servlet;

import com.shop.bean.PurchaseIntent;
import com.shop.dao.PurchaseDAO;
import com.shop.dao.TradeDAO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        if ("submit".equals(method)) {
            submitIntent(request, response);
        } else if ("cancel".equals(method)) {
            cancelIntent(request, response);
        } else if ("confirmTrade".equals(method)) {
            confirmTrade(request, response);
        }
    }

    // 提交购买意向
    private void submitIntent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PurchaseIntent intent = new PurchaseIntent();
        intent.setProductId(Integer.parseInt(request.getParameter("productId")));
        intent.setBuyerName(request.getParameter("buyerName"));
        intent.setLocation(request.getParameter("location"));
        intent.setPhone(request.getParameter("phone"));
        intent.setAmount(Double.parseDouble(request.getParameter("amount")));
        intent.setDeposit(Double.parseDouble(request.getParameter("deposit")));

        try {
            Date tradeTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(request.getParameter("tradeTime"));
            intent.setTradeTime(tradeTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        PurchaseDAO purchaseDAO = new PurchaseDAO();
        purchaseDAO.addPurchaseIntent(intent);
        request.setAttribute("msg", "购买意向提交成功，等待卖家确认");
        request.getRequestDispatcher("product.jsp").forward(request, response);
    }

    // 取消购买意向
    private void cancelIntent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int intentId = Integer.parseInt(request.getParameter("intentId"));
        PurchaseDAO purchaseDAO = new PurchaseDAO();
        purchaseDAO.updateIntentStatus(intentId, 5, "买家取消");
        request.setAttribute("msg", "已取消购买");
        request.getRequestDispatcher("product.jsp").forward(request, response);
    }

    // 买家确认交易完成
    private void confirmTrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int purchaseId = Integer.parseInt(request.getParameter("purchaseId"));
        TradeDAO tradeDAO = new TradeDAO();
        tradeDAO.updateTradeConfirm(purchaseId, "buyer");
        request.setAttribute("msg", "已确认交易完成");
        request.getRequestDispatcher("product.jsp").forward(request, response);
    }
}